package com.example.springboot3.controller;

import com.example.springboot3.pojo.User;
import com.example.springboot3.service.IUserService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpSession;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author 张琪
 * @date 2024-02-22 15:19
 */
@RestController
public class SessionController {

    @Resource
    private IUserService userService;

    private String testUserName = "admin";
    private String testPassword = "admin";

    @PostMapping("/login")
    public String login(@RequestBody User user, HttpSession session) {
        if (testUserName.equals(user.getUsername()) && testPassword.equals(user.getPassword())) {
            session.setAttribute("user", user);
            return "登录成功";
        }
        return "登录失败";
    }

    @GetMapping("/logout")
    public String logout(HttpSession session) {
        session.removeAttribute("user");
        return "退出成功";
    }


    @GetMapping("/getUser")
    public String getUser() {
        return userService.doSomething();
    }
}
